<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <title>新增友链</title>
    <script th:src="@{/assets/js/jquery.min.js}"></script>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}" />
</head>

<body>
<form id="cform" class="form-inline" method="post" href="#">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">友链标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" autocomplete="off" placeholder="请输入友链标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">友链地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="link" autocomplete="off" placeholder="请输入友链地址" class="layui-input">
                    </div>
                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
<script>
    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let jq = $.noConflict();
        // 获取<meta>标签中封装的_csrf信息 ,否则会请求403
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var headers = {"X-CSRF-TOKEN": token}
        form.on('submit(user-save)', function(data){

            $.ajax({
                url:'/pear/add/link',
                data: jq('#cform').serialize(),
                dataType:'json',
                //一定不能写contentType: xxx json ,不然响应结果就会输出到页面上，success回调函数就接收不到
                type:'post',
                headers: headers,
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
    })


</script>
</body>
</html>